﻿
using System;
using System.IO.Ports;
using System.Windows.Forms;
using Sunny.UI;
using System.Threading;
using System.Threading.Tasks;
using BaseDll;
using CommonTools;
namespace OtherDevice
{
    public partial class OtherDeviceForm : UIPage
    {
        public OtherDeviceForm()
        {
            InitializeComponent();
        }
        private void OtherDeviceForm_Load(object sender, EventArgs e)
        {
            sys.g_eventRightChanged += ChangedUserRight;
            GlobalVariable.g_eventStationStateChanged += GlobalVariable_g_eventStationStateChanged;
            GlobalVariable_g_eventStationStateChanged(GlobalVariable.g_StationState);
        }
        public void ChangedUserRight(User CurrentUser)
        {
            if (InvokeRequired)
                this.BeginInvoke(new Action(() => ChangedUserRight(CurrentUser)));
            else
            {
                if ((int)sys.g_User._userRight >= (int)UserRight.Debug && (GlobalVariable.g_StationState == StationState.StationStateEmg || GlobalVariable.g_StationState == StationState.StationStateStop))
                {
                    this.Enabled = true;
                }
                else
                {
                    this.Enabled = false;
                }
            }

        }
        private void GlobalVariable_g_eventStationStateChanged(StationState currState)
        {
            if (InvokeRequired)
                this.BeginInvoke(new Action(() => GlobalVariable_g_eventStationStateChanged(currState)));
            else
            {
                if ((int)sys.g_User._userRight >= (int)UserRight.Debug && (GlobalVariable.g_StationState == StationState.StationStateEmg || GlobalVariable.g_StationState == StationState.StationStateStop))
                {
                    this.Enabled = true;
                }
                else
                {
                    this.Enabled = false;
                }
            }

        }
        private void dg_DeviceList_CellClick(object sender, DataGridViewCellEventArgs e)
        {


        }

        private void OtherDeviceForm_Initialize(object sender, EventArgs e)
        {
            try
            {
                dg_DeviceList.Rows.Clear();
                foreach (var item in DeviceMgr.GetInstance().dic_DeviceInfo)
                {
                    dg_DeviceList.Rows.Add(item.Key, item.Value.deviceType, item.Value.deviceTypeName);

                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("加载串口数据失败:" + ex.Message);
            }

        }

        private void dg_DeviceList_SelectIndexChange(object sender, int index)
        {
            try
            {
                int n_Index = index;
                if (n_Index < 0)
                {
                    MessageBox.Show("请先选中要操作的设备"); return;
                }
                else
                {
                    string strName = dg_DeviceList.Rows[n_Index].Cells[0].Value.ToString();
                    DeviceInfo deviceInfo = DeviceMgr.GetInstance().dic_DeviceInfo[strName];
                    Form_ControlBase page = null;
                    object objParamter = null;
                    if (deviceInfo.deviceType == DeviceType.串口)
                    {
                        page = deviceInfo.Device_串口.controlFrom;
                        objParamter = deviceInfo.Device_串口;
                    }
                    else if (deviceInfo.deviceType == DeviceType.TcpIp)
                    {
                        page = deviceInfo.Device_TcpIp.controlFrom;
                        objParamter = deviceInfo.Device_TcpIp;

                    }
                    else if (deviceInfo.deviceType == DeviceType.气缸)
                    {
                        page = deviceInfo.Device_气缸.controlFrom;
                        objParamter = deviceInfo.Device_气缸;
                    }
                    panel_Device.Controls.Clear();
                    panel_Device.Controls.Add(page);
                    page.FlushToDlg(objParamter);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }
    }
}

